import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.poi.POIXMLDocument;
import org.apache.poi.hslf.extractor.PowerPointExtractor;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xslf.extractor.XSLFPowerPointExtractor;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;

import java.io.*;
import java.text.NumberFormat;

/**
 * Created by lyq on 2017/11/30.
 */

public class ReadFileUtils {

    public static void main(String[] args) throws IOException {
        ReadFileUtils readFileUtils = new ReadFileUtils();
        String s ="";
        //s = readFileUtils.readPPT("F:\\毕设资料\\林尤庆.ppt");
        //s = readFileUtils.readPDF("F:\\毕设资料\\翻译英文.pdf");
        //s = readFileUtils.readWord("F:\\毕设资料\\安卓游戏.doc");
        //s = readFileUtils.readDocx("F:\\毕设资料\\安卓游戏.docx");
        //s = readFileUtils.readPPT2007("E:\\1.pptx");
      /*  File file = new File("F:\\毕设资料\\翻译英文.pdf");
        PDDocument document = PDDocument.load(file);
        int pages =document.getNumberOfPages();//获取PDF页数

        PDFTextStripper stripper = new PDFTextStripper();
        s = stripper.getText(document);//读取PDF内容*/
        s = readFileUtils.readPDF("F:\\毕设资料\\翻译英文.pdf");
        System.out.println(s);
    }

    //读取ppt文件
    public String readPPT(String file) throws IOException {
        String re="";
        InputStream is = null;
        try{
            is = new FileInputStream(new File(file));
            PowerPointExtractor extractor = new PowerPointExtractor(is);
            re = extractor.getText();
        }catch(Exception e){
            System.out.println("读取ppt出错"+e.toString());
        }finally {
            is.close();
        }
        return re;
    }

    //读取pptx文件
    public String readPPT2007(String file) throws IOException {
        OPCPackage opc = null;
        String re = "";
        try{
            opc = POIXMLDocument.openPackage(file);
            re = new XSLFPowerPointExtractor(opc).getText();
        } catch (Exception e) {
            System.out.println("读取pptx出错"+e.toString());
        }finally {
            opc.close();
        }
        return  re;
    }

    // 读取pdf文件
   public String readPDF(String filepath) throws IOException {
       String result = null;
       PDDocument document = null;
       File file = new File(filepath);
       document = PDDocument.load(file);
       PDFTextStripper stripper = new PDFTextStripper();
       result = stripper.getText(document);
        return result;
    }

    // 读取doc文件
    public String readWord(String file){
        String result ="";
        WordExtractor wordExtractor = null;
        try{
            wordExtractor = new WordExtractor(new FileInputStream(file));
            result = wordExtractor.getText();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    // 读取docx文件
    public String readDocx(String file) throws IOException {
        String result = "";
        OPCPackage opc = null;
        try{
            opc = POIXMLDocument.openPackage(file);
            result = new XWPFWordExtractor(opc).getText();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            opc.close();
        }
        return result;
    }

    // 读取xls文件
    public String readEXCEL(String file){
        StringBuilder content = new StringBuilder();
        HSSFWorkbook workbook = null;
        try{
            workbook = new HSSFWorkbook(new FileInputStream(file));
            for(int numSheets = 0;numSheets <workbook.getNumberOfSheets();numSheets++){
                if(null != workbook.getSheetAt(numSheets)){
                    HSSFSheet sheet = workbook.getSheetAt(numSheets);
                    for(int rowNumOfSheet  = 0;rowNumOfSheet <= sheet.getLastRowNum();rowNumOfSheet++ ){
                        HSSFRow row = sheet.getRow(rowNumOfSheet);
                        for (short cellNumOfRow = 0; cellNumOfRow <=row.getLastCellNum();cellNumOfRow ++){
                            HSSFCell cell = row.getCell(cellNumOfRow);
                            if(this.convertCellHSSFCell(cell).length() > 0){
                                content.append(this.convertCellHSSFCell(cell));
                            }
                        }
                        content.append("\n");
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return content.toString();
    }

    private String convertCellHSSFCell(HSSFCell cell){
        NumberFormat format = NumberFormat.getInstance();
        format.setGroupingUsed(false);
        String cellValue = "";
        if(cell == null){
            return cellValue;
        }
        switch (cell.getCellType()){
            case HSSFCell.CELL_TYPE_NUMERIC:
                cellValue = format.format(cell.getNumericCellValue());
                break;
            case HSSFCell.CELL_TYPE_STRING:
                cellValue = cell.getStringCellValue();
                break;
            case HSSFCell.CELL_TYPE_BLANK:
                cellValue = cell.getStringCellValue();
                break;
            case HSSFCell.CELL_TYPE_BOOLEAN:
                cellValue = Boolean.valueOf(cell.getBooleanCellValue()).toString();
                break;
            case HSSFCell.CELL_TYPE_ERROR:
                cellValue = String.valueOf(cell.getErrorCellValue());
                break;
            default:
                cellValue = "";
        }
        return cellValue.trim();
    }

    // 读取xlsx文件
    public String readEXCEL2007(String file) throws IOException {
        XSSFWorkbook workbook=null;
        StringBuilder content = new StringBuilder();
        try{
            workbook = new XSSFWorkbook(file);
            for (int numSheets = 0; numSheets < workbook.getNumberOfSheets(); numSheets++) {
                if (null != workbook.getSheetAt(numSheets)) {
                    XSSFSheet aSheet = workbook.getSheetAt(numSheets);// 获得一个sheet
                    for (int rowNumOfSheet = 0; rowNumOfSheet <= aSheet.getLastRowNum(); rowNumOfSheet++) {
                        if (null != aSheet.getRow(rowNumOfSheet)) {
                            XSSFRow aRow = aSheet.getRow(rowNumOfSheet); // 获得一个行
                            for (short cellNumOfRow = 0; cellNumOfRow <= aRow.getLastCellNum(); cellNumOfRow++) {
                                if (null != aRow.getCell(cellNumOfRow)) {
                                    XSSFCell aCell = aRow.getCell(cellNumOfRow);// 获得列值
                                    if (this.convertCellXHSSFCell(aCell).length() > 0) {
                                        content.append(this.convertCellXHSSFCell(aCell));
                                    }
                                }
                                content.append("\n");
                            }
                        }
                    }
                }
            }
        }
        catch(Exception ex){
            System.out.println("读取excel出错"+ex.toString());
        }
        return content.toString();
    }


    private String convertCellXHSSFCell(XSSFCell aCell) {
        NumberFormat formater = NumberFormat.getInstance();
        formater.setGroupingUsed(false);
        String cellValue = "";
        if (aCell == null) {
            return cellValue;
        }
        switch (aCell.getCellType()) {
            case HSSFCell.CELL_TYPE_NUMERIC:
                cellValue = formater.format(aCell.getNumericCellValue());
                break;
            case HSSFCell.CELL_TYPE_STRING:
                cellValue = aCell.getStringCellValue();
                break;
            case HSSFCell.CELL_TYPE_BLANK:
                cellValue = aCell.getStringCellValue();
                break;
            case HSSFCell.CELL_TYPE_BOOLEAN:
                cellValue = Boolean.valueOf(aCell.getBooleanCellValue()).toString();
                break;
            case HSSFCell.CELL_TYPE_ERROR:
                cellValue = String.valueOf(aCell.getErrorCellValue());
                break;
            default:
                cellValue = "";
        }
        return cellValue.trim();
    }
}
